import got from "got";
import { createWriteStream } from "fs";
import stream from "stream";
import { promisify } from "util";
import path from "node:path";

const pipeline = promisify(stream.pipeline);

// const url = "https://registry.npmmirror.com/-/binary/node/v20.9.0/node-v20.9.0-win-x64.7z";
const url = "https://registry.npmmirror.com/-/binary/node/v20.9.0/SHASUMS256.txt.asc";
const fileName = path.basename(url);

const ds = got.stream(url);
const fileWriterStream = createWriteStream(fileName);

ds.on("downloadProgress", ({ transferred, total, percent }) => {
  const percentage = Math.round(percent * 100);
  console.error(`progress: ${transferred}/${total} (${percentage}%)`);
});

ds.on("end", () => {
  console.log("end");
});

ds.on("error", err => {
  console.error("error xx", err.message);
  console.error(err?.message);
  console.error(err);
});

ds.pipe(fileWriterStream);
ds.destroy();
